﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Utils;

namespace COFE.Core
{
    /// <summary>
    /// Define a user friendly name for GetSupportedProperty/ActionString().  
    /// Also used by EnumUtils.GetResourceName().
    /// </summary>
    public class AliasAttribute : Attribute
    {
        public AliasAttribute(string alias)
        {
            Alias = alias;
            FullQualifiedName = false;
        }

        public AliasAttribute(bool fullQualifiedName)
        {
            Alias = null;
            FullQualifiedName = fullQualifiedName;
        }
       
        /// <summary>
        /// Use FullQualifiedName? e.g. "FileProperties.Length"
        /// </summary>
        public bool FullQualifiedName { get; private set; }

        public string Alias { get; private set; }
    }

     /// <summary>
    /// Extends MetadataAttribute.
    /// </summary>
    public static class AliasExtension
    {

        /// <summary>
        /// Whether a property is tagged with AliasAttribute.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <returns></returns>
        public static bool IsMetadata<T>(T item)
        {
            return EnumAttributeUtils<AliasAttribute, T>.FindAttribute(item) != null;
        }

        /// <summary>
        /// Return alias of the specified property, or property.ToString() if it's not specified.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <returns></returns>
        public static string getAlias<T>(T item)
        {
            var aa = EnumAttributeUtils<AliasAttribute, T>.FindAttribute(item);

            //If Alias != null, Alias
            //If not specified or aa.FullQualifiedName = true, FullQualified Name.
            //Otherwise (aa.FullQualifiedName = false) return property name.

            if (aa == null) 
                return EnumUtils.GetFullName(item);
            if (aa.Alias != null)
                return aa.Alias;
            if (aa.FullQualifiedName)
                return EnumUtils.GetFullName(item);
            else return item.ToString();            
        }
    }
}
